clear; close all; clc;

%% load results


cdir = pwd;
savef = 1;                  % savef = 1 to save plots
fdir  = 'Figures/ParetoFrontier';  % location to save plots

idcase = 1; % idcase = 1 for Argentina, idcase = 2 for Spain
if idcase == 1
    cname = 'Argentina';
elseif idcase == 2
    cname = 'Spain';
else
    disp('set idcase=1 for Argenta or idcase=2 for Spain')
    messi
end

odir  = strcat(cname,'/OUTPUT_1');  % benchmark calibration 

cd(odir)

load bvec.txt;
load zvec.txt;

load Vnd.txt;
load dpol.txt;
load Qprice.txt;

load prms.txt;

%-Simulation moments
load bsim.txt;
load gsim.txt;
load idsim.txt;

cd(cdir)

%prms = [Rs,dlta,ybarL,ybarH,kppa,pgL,pgH,bta,gL,gH,sdz,psB]
Rs  = prms(1) ; dlta = prms(2) ; ybarL = prms(3) ; ybarH = prms(4) ; kppa  = prms(5);
pgL = prms(6) ; pgH  = prms(7) ; bta   = prms(8) ; gL    = prms(9) ; gH    = prms(10); 
sdz = prms(11); psB   = prms(12);

psG   = 1.0-psB;

gvec = [gL, gH];
Pg   = [pgL  , 1-pgL; ...
        1-pgH, pgH];
Ps   = [psB  , 1-psB; ...
        1-psG, psG];

Nb = numel(bvec); Nz = numel(zvec); Ng = 2; Ns = 2;

gma = 3.0;  % CRRA coefficient
 
%% reshape results

Vndx = Vnd; Qpricex = Qprice; dpolx = dpol;

Vnd  = 189*ones(Nb,Ng,Ns,Nz); Qprice = 189*ones(Nb,Ng,Ns,Nz);
dpol = 189*ones(Nb,Ng,Ns,Nz);

inn = 0;
for ig = 1:Ng
for is = 1:Ns
for iz = 1:Nz
    for ib = 1:Nb
        inn = inn+1;
        Vnd(ib,ig,is,iz)  = Vndx(inn);
        dpol(ib,ig,is,iz) = dpolx(inn);
    end
end
end
end

inn = 0;
for ig = 1:Ng
for is = 1:Ns
for ib = 1:Nb    
for iz = 1:Nz
    inn = inn+1;
    Qprice(ib,ig,is,iz) = Qpricex(inn);
end
end
end
end

exg  = 1/(1-gma); 
cxnd = 0*Vnd;
for ig = 1:Ng
for is = 1:Ns    
for ib = 1:Nb    
for iz = 1:Nz
    num = gvec(ig) * exp(zvec(iz));
    xx  = (Vnd(ib,ig,is,iz)*(1-gma)*(1-bta))^exg;
    cxnd(ib,ig,is,iz) = xx/num;
end
end
end
end
%% plot set-up

ccol  = [0.8 0.4 0.4];

ARGf = 1;

linesz   = 0.5;
labelsz  = 9;
legendsz = 9;
numsz    = 9;
textsz   = 9;

%% computations

%-Excluding first Tburn periods
Tburn  = 1000;
idsim  = idsim(2:end);
bsim   = bsim(Tburn+1:end);
gsim   = gsim(Tburn+1:end);
idsim  = idsim(Tburn+1:end);

innx = (idsim==0).*(gsim==gL);
bsim_ndgL = bsim(innx==1); 
Nsim_ndgL = numel(bsim_ndgL);


%% plots set-up

ig = 1; iz = 9;

bLB = 0.155; bUB = 0.212;  %ARG
%bLB = 0.1313; bUB = 0.181;  %SPA
%bLB = 0.01; bUB = 0.55;

ddpp = [dpol(:,ig,1,iz), dpol(:,ig,2,iz)];
	

bvecx = bvec./(gvec(ig)*exp(zvec(iz)));

%% plots

fig = figure(101); clf
is=1;plot(bvec,squeeze(Vnd(:,ig,is,iz)),'LineWidth',linesz,'Color',ccol,'LineStyle','--'); hold on
is=2;plot(bvec,squeeze(Vnd(:,ig,is,iz)),'LineWidth',linesz,'Color',ccol);
set(gca,'XGrid','off','YGrid','off','Fontsize',numsz)
xlim([bLB bUB])
xlabel('$b$','Interpreter','LaTex','Fontsize',labelsz)
ylabel('$v^{nd}(b,\cdot)$','Interpreter','LaTex','Fontsize',labelsz)
leg = legend('bad sunspot','good sunspot');
set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','NorthEast')
legend boxoff
hold off
if savef == 1
    paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
    paper_size = [3.0 2.0]; % [width height]
    paper_position = [0 0 3.0 2.0]; % [left bottom width height].
    paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
    fig = gcf;
    fig.PaperUnits = paper_unit;
    fig.PaperSize = paper_size;
    fig.PaperPosition = paper_position;
    fig.PaperOrientation = paper_orientation;    
    cd(fdir)
    saveas(gcf,strcat('PARETO_Vnd_',cname,'.pdf'))    
    cd(cdir)    
end

fig = figure(102); clf
is=1;plot(bvec,Qprice(:,ig,is,iz),'LineWidth',linesz,'Color',ccol,'LineStyle','--'); hold on
is=2;plot(bvec,Qprice(:,ig,is,iz),'LineWidth',linesz,'Color',ccol);
set(gca,'XGrid','off','YGrid','off','Fontsize',numsz)
xlim([bLB bUB])
xlabel('$b$','Interpreter','LaTex','Fontsize',labelsz)
ylabel('$Q(b,\cdot)$','Interpreter','LaTex','Fontsize',labelsz)
%leg = legend('bad sunspot','good sunspot');
%set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','NorthEast')
%legend boxoff
hold off
if savef == 1    
    paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
    paper_size = [3.0 2.0]; % [width height]
    paper_position = [0 0 3.0 2.0]; % [left bottom width height].
    paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
    fig = gcf;
    fig.PaperUnits = paper_unit;
    fig.PaperSize = paper_size;
    fig.PaperPosition = paper_position;
    fig.PaperOrientation = paper_orientation;
    cd(fdir)
    saveas(gcf,strcat('PARETO_Q_',cname,'.pdf'))    
    cd(cdir)    
end

fig = figure(103); clf
if idcase == 1
    plot([0.2 0.2],[13.00 17.45],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--'); hold on
    plot([bLB 0.2],[15.00 15.00],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--')
    plot([bLB 0.2],[17.45 17.45],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--')

    text(0.20025, 15.250,'$B$','Interpreter','LaTex','Fontsize',textsz) %text(05,0.15+0.040,'Target Upper Limit','Fontsize',23,'Interpreter','LaTex') 
    text(0.20025, 17.705,'$G$','Interpreter','LaTex','Fontsize',textsz)
end

is=1;plot(bvec,100*Qprice(:,ig,is,iz).*bvecx,'LineWidth',linesz,'Color',ccol,'LineStyle','--'); hold on
is=2;plot(bvec,100*Qprice(:,ig,is,iz).*bvecx,'LineWidth',linesz,'Color',ccol);

set(gca,'XGrid','off','YGrid','off','Fontsize',numsz)
xlim([bLB bUB])
xlabel('$b$','Interpreter','LaTex','Fontsize',labelsz)
ylabel('$\mathcal{M}(b,\cdot) = Q(b,\cdot) \times b$ (\% of output)','Interpreter','LaTex','Fontsize',labelsz)
if idcase == 1
    ylim([13 18.50])
    %leg = legend('','','','bad sunspot','good sunspot');
    %set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','SouthEast')
    %legend boxoff
else
    %leg = legend('bad sunspot','good sunspot');
    %set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','NorthWest')
    %legend boxoff
end

hold off
if savef == 1
    %run graph_extended.m
    paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
    paper_size = [3.0 2.0]; % [width height]
    paper_position = [0 0 3.0 2.0]; % [left bottom width height].
    paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
    fig = gcf;
    fig.PaperUnits = paper_unit;
    fig.PaperSize = paper_size;
    fig.PaperPosition = paper_position;
    fig.PaperOrientation = paper_orientation;
    cd(fdir)
    saveas(gcf,strcat('PARETO_Qb_',cname,'.pdf'))    
    cd(cdir)    
end

qxx = [Qprice(:,ig,1,iz),Qprice(:,ig,1,iz)];

%% frontier

ibLB = find(bvec<=bLB,1,'last'); ibUB = find(bvec<=bUB,1,'last'); 
is=1; xx=Vnd(ibLB:ibUB,ig,is,iz); yy=Qprice(ibLB:ibUB,ig,is,iz);yy=yy.*bvecx(ibLB:ibUB); dd=dpol(ibLB:ibUB,ig,is,iz); xx=xx(dd<0.1);yy=yy(dd<0.1); xxs1=xx; yys1=yy; dds1=dd;
is=2; xx=Vnd(ibLB:ibUB,ig,is,iz); yy=Qprice(ibLB:ibUB,ig,is,iz);yy=yy.*bvecx(ibLB:ibUB); dd=dpol(ibLB:ibUB,ig,is,iz); xx=xx(dd<0.1);yy=yy(dd<0.1); xxs2=xx; yys2=yy; dds2=dd;

%bb = bvec(ibLB:ibUB); bbs1=bb(dds1<1.1); Ns1 = numel(bbs1); PDFs1=data2PDF(bsim_ndgL,ones(Nsim_ndgL,1),bbs1);
bbs1 = linspace(0.05,0.30,25)'; 

fig = figure(1013); clf
if idcase == 1
    plot([-2.29 -2.29],[13.00 17.70],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--'); hold on
    plot([-2.32 -2.29],[15.00 15.00],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--')
    plot([-2.32 -2.29],[17.70 17.70],'color',[0.1 0.1 0.1],'LineWidth',linesz*0.9,'LineStyle','--')

    text(-2.29, 15.05,'$P_{B}$','Interpreter','LaTex','Fontsize',textsz) %text(05,0.15+0.040,'Target Upper Limit','Fontsize',23,'Interpreter','LaTex') 
    text(-2.29, 17.80,'$P_{G}$','Interpreter','LaTex','Fontsize',textsz)
end

plot(xxs1,100*yys1,'LineWidth',linesz,'Color',ccol,'LineStyle','--'); hold on
plot(xxs2,100*yys2,'LineWidth',linesz,'Color',ccol)

set(gca,'XGrid','off','YGrid','off','Fontsize',numsz)
xlabel('Borrower value $v^{nd}(b,\cdot)$','Interpreter','LaTex','Fontsize',labelsz)
ylabel('Lender value: $\mathcal{M}(b,\cdot)$ (\% of output)','Interpreter','LaTex','Fontsize',labelsz)
%xlim([0 40])
%ylim([0 40])
if idcase == 1
    ylim([13 18.50])
    leg = legend('','','','bad sunspot','good sunspot');
    set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','NorthEast')
else
    leg = legend('bad sunspot','good sunspot');
    set(leg,'Interpreter','LaTex','Fontsize',legendsz,'Location','NorthEast')
end
legend boxoff

hold off
if savef == 1    
    paper_unit = 'inches'; % 'inches', 'normalized', 'centimeters', 'points'
    paper_size = [5.0 2.0]; % [width height]
    paper_position = [0 0 5.0 2.0]; % [left bottom width height].
    paper_orientation = 'portrait'; % 'portrait (default)', 'landscape'
    fig = gcf;
    fig.PaperUnits = paper_unit;
    fig.PaperSize = paper_size;
    fig.PaperPosition = paper_position;
    fig.PaperOrientation = paper_orientation;
    cd(fdir)
    saveas(gcf,strcat('PARETO_FRONTIER_',cname,'.pdf'))    
    cd(cdir)    
end


